<?
if (!defined("B_PROLOG_INCLUDED") || B_PROLOG_INCLUDED!==true)die();

require_once($_SERVER["DOCUMENT_ROOT"].$componentPath."/functions.php");

if (!CModule::IncludeModule("support"))
{
	ShowError("Модуль техподдержки не установлен.");
	return;
}

//Permissions

if ( !($USER->IsAuthorized() && (CTicket::IsSupportClient() || CTicket::IsAdmin() || CTicket::IsSupportTeam())) )
	$APPLICATION->AuthForm(GetMessage("ACCESS_DENIED"));

$arParams["CODE"] = $_REQUEST[$arParams["VARIABLE_ALIASES"]["ID"]]; 

$strError = "";

// post

if ($_SERVER["REQUEST_METHOD"]=="POST" && check_bitrix_sessid())
{
	if (strlen($_REQUEST["TITLE"])==0)
		$strError .= "Поле &laquo;Заголовок&raquo; должно быть заполнено.";
	
	$arFILES = array();
	if (is_array($_FILES) && count($_FILES)>0)
	{
		foreach ($_FILES as $key => $arFILE)
		{
			if (strlen($arFILE["name"])>0)
			{
				$arFILE["MODULE_ID"] = "support";
				$arFILES[] = $arFILE;
			}
		}
	}

	if (is_array($arFILES) && count($arFILES)>0)
	{
		$max_size = COption::GetOptionString("support", "SUPPORT_MAX_FILESIZE");
		$max_size = intval($max_size)*1024;

		foreach ($arFILES as $key => $arFILE)
		{
			if (intval($arFILE["size"])>$max_size || intval($arFILE["error"])>0)
				$strError .= str_replace("#FILE_NAME#", $arFILE["name"], GetMessage("SUP_MAX_FILE_SIZE_EXCEEDING"))."<br>";
		}
	}
	
	if ($strError == "")
	{
		$MESSAGE = "";
		for ($n= 0; $n < count($_REQUEST["ZAG"]); $n++)
			$MESSAGE .= $_REQUEST["ZAG"][$n].":\n".$_REQUEST["ITEM"][$n]."\n\n";
		


		$arFields = array(
			'SITE_ID'					=> SITE_ID,
			'CLOSE'						=> "N",
			'TITLE'						=> $_REQUEST['TITLE'],
			'CRITICALITY_ID'			=> $_REQUEST['CRITICALITY_ID'],
			'CATEGORY_ID'				=> $_REQUEST['CATEGORY_ID'],			
			'MESSAGE'					=> $MESSAGE,
			'HIDDEN'						=> 'N',
			'FILES'						=> $arFILES,
			'PUBLIC_EDIT_URL'			=> $APPLICATION->GetCurPage(),
		);

		//print_r($arFields); exit;

		$ID = CTicket::SetTicket($arFields, $ID, "Y", $NOTIFY = "Y");
		if (intval($ID)>0)
		{
			LocalRedirect($arParams["TICKET_LIST_URL"]);
		}
		else 
		{
			$ex = $APPLICATION->GetException();
			if ($ex)
			{
				$strError .= $ex->GetString() . '<br>';
			}
			else 
			{
				$strError .= GetMessage('SUP_ERROR') . '<br>';
			}
		}
	}	
}


// result

$arResult = Array(
	"ITEMS" => Array(),
	"DICTIONARY" => Array(
		"CRITICALITY" => Array(),
	),
	"ERROR_MESSAGE" => $strError,
	"REAL_FILE_PATH" => htmlspecialchars($APPLICATION->GetCurPageParam()),
	"OPTIONS" => Array(
		"MAX_FILESIZE" => intval(COption::GetOptionString("support", "SUPPORT_MAX_FILESIZE")),
	),
);

$ticketSite = SITE_ID;
$ticketSla = CTicketSLA::GetForUser();
$ticketDictionary = CTicketDictionary::GetDropDownArray($ticketSite, $ticketSla);
$arResult["DICTIONARY"]["CRITICALITY_DEFAULT"] = CTicketDictionary::GetDefault("K", $ticketSite);
$arResult["DICTIONARY"]["CRITICALITY"] = _GetDropDownDictionary("K", $ticketDictionary);


// выбор раздела Мастера обращений
$sects = CIBlockSection::GetList(array("ID"=>"ASC"), array("IBLOCK_TYPE"=>$arParams["IBLOCK_TYPE"], "IBLOCK_ID"=>$arParams["IBLOCK_ID"], "CODE"=>$arParams["CODE"], "ACTIVE"=>"Y"));
if ( $sect = $sects->GetNext())
{
	// определение категории обращения
	if (isset($arParams["SECTION_".$sect["ID"]]))
		$arResult["CATEGORY_ID"] = $arParams["SECTION_".$sect["ID"]];
	else
		$arResult["CATEGORY_ID"] = 0;
	
	// определение разделов заявки
	$arResult["SECT_CHILD"] = $sect["NAME"];
	$psects = CIBlockSection::GetList(array("ID"=>"ASC"), array("IBLOCK_TYPE"=>$arParams["IBLOCK_TYPE"], "IBLOCK_ID"=>$arParams["IBLOCK_ID"], "ID"=>$sect["IBLOCK_SECTION_ID"], "ACTIVE"=>"Y"));
	if ( $psect = $psects->GetNext() )
		$arResult["SECT_PARENT"] = $psect["NAME"];
		
	// выбор элементов вопросов
	$items = CIBlockElement::GetList(
		Array("SORT"=>"ASC", "ID"=>"ASC"),
		Array("IBLOCK_TYPE"=>$arParams["IBLOCK_TYPE"], "IBLOCK_ID"=>$arParams["IBLOCK_ID"], "SECTION_ID"=>$sect["ID"], "ACTIVE"=>"Y")
	);
	while ( $obElement = $items->GetNextElement() )
	{
		$item = $obElement->GetFields();
		$item["PROP"] = $obElement->GetProperties();
		$arResult["ITEMS"][] = $item;
	}
	
	
}

//echo "<h2>REQUEST</h2>";
//echo "<pre>";
//print_r($_REQUEST);
//echo "</pre>";
//
//echo "<h2>arParams</h2>";
//echo "<pre>";
//print_r($arParams);
//echo "</pre>";
//
//echo "<h2>arResult</h2>";
//echo "<pre>";
//print_r($arResult);
//echo "</pre>";


$this->IncludeComponentTemplate();

?>